<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.sales.forms.SalesOrderForm,
                 com.scalar.chiptrack.sales.CustomerPartNumber,
                 java.util.HashMap,
                 com.scalar.chiptrack.sales.SalesOrder"%>
<%@ page import="com.scalar.chiptrack.commons.Constants" %>
<%@ page import="java.util.ArrayList" %>
<%
    String chipTracContext = "/" + ConfigManager.getChipTracContext();
    String statuss = "";
    String finalstatus = "";
    int i = 0;
    boolean isCancel = false;
    String oldStatus = "";
    boolean disableLineItem = false;
    String userTypeChiptrac = Constants.USER_TYPE_CHIPTRAC + "";
    boolean isChiptracUser = true;
    String noOfDecimalsInAmount = ConfigManager.getnoOfDecmalsInAmount()+"";
    String restrictCharsJSFnc = "restrictCharacters(this,7,"+noOfDecimalsInAmount+")";
    String restrictAmtJSFnc = "restrictAmt(this,7,"+noOfDecimalsInAmount+")";
%>
<logic:notEqual value="<%=userTypeChiptrac%>" name="user" property="userType" scope="session">
    <%
        isChiptracUser = false;
    %>
</logic:notEqual>
<logic:iterate id="myHashMap" name="lineItemsWithSubtotals" >
<logic:iterate id="lineItemList" name="myHashMap" >
<logic:iterate id="LineItem" name="lineItemList" indexId="iterateIdx" property="key" type="com.scalar.chiptrack.sales.LineItem">
    <%
        try {
            statuss = LineItem.getStatus();
            finalstatus = LineItem.getFinalStatus();
            oldStatus = LineItem.getOldStatus();
            isCancel = LineItem.getIsCancel();
            if ( !isChiptracUser && "APPROVED".equalsIgnoreCase(statuss) )
            {
                disableLineItem = true;
            }
            else
            {
                disableLineItem = false;
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    %>
    <input type='hidden' name="issueStatus" value="<%=LineItem.getIssueStatus()%>">
    <input type='hidden' name="errorStatus" value="">
    <input type='hidden' name="checkStatus" value="<%=finalstatus%>">
    <input type='hidden' name="cancelStatus" value="<%=( ( LineItem.getIsCancel() == true ) ? 0 : 1 )%>">
    <tr>
	    <td width="8%" bgcolor="#DEE1E2">
            <input type="text" name="lineItemNo" value="<%=LineItem.getLineItemNo()%>" size='6' Class="txfield" disabled="true" >
            <input type="hidden" name="lineItemId" value="<%=LineItem.getLineItemId()%>" >
            <input type="hidden" name="scheduleDate" value="<%=LineItem.getScheduleDate()%>" >
            <input type="hidden" name="revisedDate" value="<%=LineItem.getRevisedDate()%>" >
            <input type='hidden' name="statusq" value="<%=statuss%>">
            <input type='hidden' name="oldstatusq" value="<%=oldStatus%>">
            <input type="hidden" name="isNewLineItem" value="<%=LineItem.getIsNewLineItem()%>">
            <input type='hidden' name="finalStatus" value="<%=finalstatus%>">
        </td>
        <td width="8%" bgcolor="#DEE1E2">
        <!--For disabling the PO line item for the items which are shipped,partially shipped or cancelled-->
        <%
            String strPoLineItemNo = "";
            if( LineItem.getPoLineItemNo() == 0 )
            {
                strPoLineItemNo = "";
            }
            else
            {
                strPoLineItemNo=String.valueOf(LineItem.getPoLineItemNo());
            }
            if(("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || isCancel || disableLineItem )
            {
                out.println("<input type='text' name='poLineItemNo' size='6' value='"+strPoLineItemNo+"' class='txfield' maxlength='4' disabled='true'>");
            }
            else
            {
                out.println("<input type='text' name='poLineItemNo' size='6' value='"+strPoLineItemNo+"' class='txfield' maxlength='4'>");
            }
        %>
        </td>
        <logic:equal name="LineItem" property="isEditable" value="false">
        <td  bgcolor="#DEE1E2" nowrap>
        <% if( !disableLineItem && ( LineItem.getIsNewLineItem() || "HOLD".equalsIgnoreCase( LineItem.getStatus() ) || "APPROVED".equalsIgnoreCase( LineItem.getFinalStatus() ) ) ) { %>
            <input type="text" name="qs<%=i+1%>" class="txfield" size='35' onkeyup="quickSearch(qs_<%=i+1%>)"/><br>
            <select name="manufacturingPartNo" class="txfield" onChange="getComments(<%=i%>);<% if(ConfigManager.getBooleanProperty(ConfigManager.PROP_SHOW_SALES_LINEITEM_MARKETING_PART_NUMBER)) { %>showMktgPartNumber(<%=i%>);<%}%>">
                <option value="" >Select Part Number</option>
        <%
            java.util.ArrayList mfgList = null;
            HashMap  salesOrderMap = ( HashMap ) session.getAttribute( "salesOrderMap" );
            SalesOrder salesOrderFromMap = null;
            if ( salesOrderMap != null )
            {
                SalesOrderForm salesOrderForm1 = ( SalesOrderForm ) request.getAttribute("salesOrderForm");
                salesOrderFromMap = ( SalesOrder ) salesOrderMap.get( salesOrderForm1.getCustomerId()+"" );
                if( salesOrderFromMap != null )
                {
                    mfgList = salesOrderFromMap.getMfgParts();
                }
            }
            CustomerPartNumber customerPartNo = new  CustomerPartNumber();
            StringBuffer strBuffer1 = new StringBuffer();
            StringBuffer productsScript = new StringBuffer();
            for( int k = 0; k < mfgList.size(); k++ )
            {
                customerPartNo = ( CustomerPartNumber ) mfgList.get( k );
                if( ( customerPartNo.getManufacturingPartNo() ).equalsIgnoreCase( LineItem.getManufacturingPartNo() ) )
                {
                    strBuffer1.append( "<option value='" ).append( customerPartNo.getManufacturingPartNo() ).append( "'selected>" );
                }
                else
                {
                    strBuffer1.append( "<option value='" ).append( customerPartNo.getManufacturingPartNo() ).append( "'>" );
                }
                strBuffer1.append(customerPartNo.getManufacturingPartNo()).append("</option>\n");
                productsScript.append("ct.product.register('"+customerPartNo.getManufacturingPartNo()+"', new ct.Product('"+customerPartNo.getManufacturingPartNo()+"','"+customerPartNo.getMarketingPartNo()+"'));");
            }
            out.println(strBuffer1);
            if(i == 0) {
                out.println("<script>" +productsScript + "</script>");
            }
        %>
            </select>
            <%--<a href="javascript:partNoGenerator('<%= iterateIdx.intValue() %>')" ><IMG border="0" src="<%=chipTracContext%>/jsp/includes/images/uparrow.gif" width="20" height="16"></a>--%>
            <input type="hidden" name="partLineItemNo" value="">
        <% } else { %>
        <html:text name="LineItem" property="manufacturingPartNo" size='25' styleClass="txfield" disabled="true"/>
        <% } %>
        </td>
        <% if(ConfigManager.getBooleanProperty(ConfigManager.PROP_SHOW_SALES_LINEITEM_MARKETING_PART_NUMBER)) { %>
            <td width="15%" bgcolor="#DEE1E2"><span class="NArialL" id="marketingPartNoSpan<%=i%>"><bean:write name="LineItem" property="marketingPartNo"/></span></td>
         <% } %>
        <!-- To show customer part no details -->
        <td width="15%" bgcolor="#DEE1E2">
            <% if( ("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || ( isCancel ) || disableLineItem ) { %>
                <html:text name="LineItem" property="customerPartNo" size='25' styleClass="txfield" disabled="true" />
            <% } else { %>
            <select name="customerPartNo"  class="txfield" onchange="showHideNewCustPartNo(<%=i%>)">
                  <option value="">Select Customer Part No</option>
             <%
                 ArrayList customerPartNumbersList = null;
                 customerPartNumbersList = (ArrayList) request.getAttribute("customerPartNumbers" + LineItem.getLineItemNo());
                 if (customerPartNumbersList != null) {
                     StringBuffer strBuffer2 = new StringBuffer();
                     if (!"".equalsIgnoreCase(LineItem.getCustomerPartNo()) && !customerPartNumbersList.contains(LineItem.getCustomerPartNo())) {
                         strBuffer2.append("<option value='").append(LineItem.getCustomerPartNo()).append("'selected>").append(LineItem.getCustomerPartNo()).append("</option>\n");
                     }
                     for (int j = 0; j < customerPartNumbersList.size(); j++) {
                         String customerPartNumber = (String) customerPartNumbersList.get(j);
                         if ((customerPartNumber).equalsIgnoreCase(LineItem.getCustomerPartNo())) {
                             strBuffer2.append("<option value='").append(customerPartNumber).append("'selected>");
                         } else {
                             strBuffer2.append("<option value='").append(customerPartNumber).append("'>");
                         }
                         strBuffer2.append(customerPartNumber).append("</option>\n");
                     }
                     out.println(strBuffer2);
                 }
             %>
             <option value="-2" <%="-2".equals(LineItem .getCustomerPartNo()) ? "selected" : ""%> >--Add New CustomerPart No--</option>
             </select>
           <% } %>
            <html:text name="LineItem" property="newCustomerPartNo" style="display:none;" styleClass="txfield" maxlength="50" size="20"/>
        </td>
        <td width="15%" bgcolor="#DEE1E2">
            <% if( ("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || disableLineItem ) { %>
            <html:textarea name="LineItem" property="lineItemDescription"  styleClass="txfield" disabled="true" />
            <% } else { %>
            <html:textarea name="LineItem" property="lineItemDescription"  styleClass="txfield" disabled='<%=isCancel%>' onkeydown="javascript:setTextAreaMaxLength(this,200)" onkeyup="javascript:setTextAreaMaxLength(this,200)"/>
            <% } %>
        </td>
        </logic:equal>
        <logic:equal name="LineItem" property="isEditable" value="true">
        <td width="18%" bgcolor="#DEE1E2">
            <html:text name="LineItem" property="manufacturingPartNo" size='25' />
        </td>
        <td width="15%" bgcolor="#DEE1E2">
            <html:text name="LineItem" property="customerPartNo" size='18' styleClass="txfield"/>
        </td>
        <td width="15%" bgcolor="#DEE1E2">
            <html:text name="LineItem" property="lineItemDescription" size='18' styleClass="txfield" onkeydown="javascript:setTextAreaMaxLength(this,200)" onkeyup="javascript:setTextAreaMaxLength(this,200)"/>
        </td>
        </logic:equal>
        <td width="1%" bgcolor="#DEE1E2" align=center>
            <% if( ("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || ( request.getParameter("isSample").equals("0") ) || disableLineItem ) { %>
            <logic:equal name="LineItem" property="isTaxable" value="true">
            <input type="checkbox" name="isTaxable<bean:write name="LineItem" property="lineItemNo"/>" value="on" CHECKED disabled='true'>
            </logic:equal>
            <logic:equal name="LineItem" property="isTaxable" value="false">
            <input type="checkbox" name="isTaxable<bean:write name="LineItem" property="lineItemNo"/>" value="on" disabled='true'>
            </logic:equal>
            <% } else { %>
            <logic:equal name="LineItem" property="isTaxable" value="true">
            <input type="checkbox" name="isTaxable<bean:write name="LineItem" property="lineItemNo"/>" value="on" CHECKED <%if(isCancel == true){%> disabled="disabled"<%}%>>
            </logic:equal>
            <logic:equal name="LineItem" property="isTaxable" value="false">
            <input type="checkbox" name="isTaxable<bean:write name="LineItem" property="lineItemNo"/>" value="on" <%if(isCancel == true){%> disabled="disabled"<%}%>>
            </logic:equal>
            <% } %>
        </td>
        <td width="17%" bgcolor="#DEE1E2"  nowrap>
            <% if(("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || disableLineItem ) { %>
            <html:text name="LineItem" property="requestDate" size='15' styleClass="txfield"  disabled='true' />&nbsp;&nbsp;&nbsp;&nbsp;
            <% } else { %>
            <html:text name="LineItem" property="requestDate" size='15' styleClass="txfield"  disabled='<%=isCancel%>' />
            <% if( !isCancel ) { %>
            <a href="javascript:if(document.salesOrderForm.requestDate.length!=null){openCalendar(document.salesOrderForm.requestDate[<%= i %>])}else{openCalendar(document.salesOrderForm.requestDate)}"><img onMouseOver="window.status='Click here to select date';return true" border="0" align='center' src="<%=chipTracContext%>/jsp/includes/images/date_selector.gif" height=20 width=20></a>
            <% } else {%>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <% } } %>
        </td>
        <td width="5%" bgcolor="#DEE1E2">
            <input type="hidden" name="oldQty" value="<%=LineItem.getQuantity()%>">
            <% String a = "javascript:checkBaseOrderQty( '" + i + "' )"; %>
            <% if(("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || disableLineItem ){ %>
            <html:text name="LineItem" property="quantity"  size='8' styleClass="valuetxfield" disabled='true' onkeypress="javascript:allowInteger()" onblur='<%=a%>' />
            <% } else { %>
            <html:text name="LineItem" property="quantity"  size='8' styleClass="valuetxfield" onkeypress="javascript:allowInteger()" maxlength="6" disabled='<%=isCancel%>' onblur='<%=a%>' />
            <% } %>
           </td>
            <td width="10%" bgcolor="#DEE1E2">
            <% if(("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || disableLineItem ) { %>
            <html:text name="LineItem" property="unitPriceAsDouble" size='13' styleClass="valuetxfield" disabled='true' maxlength="9" onkeypress="<%=restrictCharsJSFnc%>" onkeyup="<%=restrictAmtJSFnc%>"/>
            <% } else { %>
            <html:text name="LineItem" property="unitPriceAsDouble" size='13' styleClass="valuetxfield" disabled='<%=isCancel%>' maxlength="9" onkeypress="<%=restrictCharsJSFnc%>" onkeyup="<%=restrictAmtJSFnc%>" />
            <% } %>
        </td>
        <td width="15%" bgcolor="#DEE1E2">
            <html:text name="LineItem" property="amountAsString" size='25' styleClass="valuetxfield" disabled="true"/>
        </td>
        <td width="15%" bgcolor="#DEE1E2" align="center">
            <% if(("SHIPPED".equalsIgnoreCase(statuss)) || ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || disableLineItem ) { %>
            <logic:equal name="LineItem" property="unitPriceAsDouble" value="0.0">
            <input type="checkbox" name="zeroPrice<bean:write name="LineItem" property="lineItemNo"/>" value="on" CHECKED disabled='true'>
            </logic:equal>
            <logic:notEqual name="LineItem" property="unitPriceAsDouble" value="0.0">
            <input type="checkbox" name="zeroPrice<bean:write name="LineItem" property="lineItemNo"/>" value="on" disabled='true'>
            </logic:notEqual>
            <% } else { %>
            <logic:equal name="LineItem" property="unitPriceAsDouble" value="0.0">
                    <% String action = request.getParameter("action");
                       if ( action != null && "loadCustomerPartNumbers".equals ( action ) ) {
                    %>
                        <input type="checkbox" name="zeroPrice<bean:write name="LineItem" property="lineItemNo"/>" value="on" onClick="javascript:setZeroPrice( <%=i%>, <bean:write name="LineItem" property="lineItemNo"/> )" <%if(isCancel == true){%> disabled="disabled"<%}%>>
                    <% }else {%>
                        <input type="checkbox" name="zeroPrice<bean:write name="LineItem" property="lineItemNo"/>" value="on" onClick="javascript:setZeroPrice( <%=i%>, <bean:write name="LineItem" property="lineItemNo"/> )" CHECKED <%if(isCancel == true){%> disabled="disabled"<%}%>>
                    <% }%>
            </logic:equal>
            <logic:notEqual name="LineItem" property="unitPriceAsDouble" value="0.0">
            <input type="checkbox" name="zeroPrice<bean:write name="LineItem" property="lineItemNo"/>" value="on" onClick="javascript:setZeroPrice( <%=i%>, <bean:write name="LineItem" property="lineItemNo"/> )" <%if(isCancel == true){%> disabled="disabled"<%}%>>
            </logic:notEqual>
            <% } %>
        </td>
        <td width="15%" bgcolor="#DEE1E2" align="center">
            <% if (("SHIPPED".equalsIgnoreCase(statuss)) || ("ISSUED".equalsIgnoreCase(finalstatus)) || disableLineItem ) { %>
            <logic:equal name="LineItem" property="status" value="Cancellation needs approval">
            <input type="checkbox" name="cancel<%=i%>" value="on" CHECKED disabled='true'>
            </logic:equal>
            <logic:notEqual name="LineItem" property="status" value="Cancellation needs approval">
            <input type="checkbox" name="cancel<%=i%>" value="on" disabled='true'>
            </logic:notEqual>
            <% } else { %>
            <logic:equal name="LineItem" property="isCancel" value="true" >
            <input type="checkbox" name="cancel<%=i%>" value="on" CHECKED  onClick="javascript:disableIfChecked(<%=i%>,<bean:write name="LineItem" property="lineItemNo"/>)" <% if( LineItem.getCancelStatus() && ("PARTIALLY SHIPPED".equalsIgnoreCase(finalstatus))){ %> disabled="disabled"<%}%>>
            </logic:equal>
            <logic:equal name="LineItem" property="isCancel" value="false" >
            <input type="checkbox" name="cancel<%=i%>" value="on" onClick="javascript:disableIfChecked(<%=i%>,<bean:write name="LineItem" property="lineItemNo"/>)" <% if( LineItem.getLineItemId() == -1){ %> disabled="disabled"<%}%>>
            </logic:equal>
            <% } %>
        </td>
        <td bgcolor="#DEE1E2" align="center">
            <% if (LineItem.getIsExpedite()) { %>
            <input type="checkbox" name="expediteFee<%=i%>" value="on" CHECKED  >
            <% } else { %>
            <input type="checkbox" name="expediteFee<%=i%>" value="on" >
            <% } %>
        </td>
        <%
             if(ConfigManager.getBooleanProperty(ConfigManager.PROP_SHOW_SALES_LINEITEM_CUSTOM_PRICE_FIELD1)) {
         %>
            <td width="10%" bgcolor="#DEE1E2">
                <html:text name="LineItem" property="customPriceField1" size='13' styleClass="valuetxfield" disabled='<%=isCancel%>' maxlength="9" onkeypress="<%=restrictCharsJSFnc%>" onkeyup="<%=restrictAmtJSFnc%>" />
            </td>
         <%
             }
             if(ConfigManager.getBooleanProperty(ConfigManager.PROP_SHOW_SALES_LINEITEM_CUSTOM_TEXT_FIELD1)) {
         %>
            <td width="10%" bgcolor="#DEE1E2">
                <font class="NArialL">
                      <html:textarea name="LineItem" property="customTextField1"  styleClass="txfield" disabled='<%=isCancel%>' onkeydown="javascript:setTextAreaMaxLength(this,200)" onkeyup="javascript:setTextAreaMaxLength(this,200)"/>
                </font>
            </td>
         <%
             }
         %>
    </tr>
    <% i = i + 1; %>
    <SCRIPT language="JavaScript">
    var rowCount = "<%=i%>";
    </SCRIPT>
    </logic:iterate>
    <%
        int noOfColumnsBeforeQty = Integer.parseInt(request.getParameter("noOfColumnsBeforeQty"));
    %>
    <tr>
        <td colspan="<%=noOfColumnsBeforeQty%>" width="15%" bgcolor="#FFFFFF" class="NArialL" align="right">Sub Total</td>
        <bean:define id="subTotal" name="lineItemList" property="value" type="com.scalar.chiptrack.sales.SubTotal"/>
        <td colspan="1" width="15%" bgcolor="#FFFFFF" class="NArialL" align="right">
            <bean:write name="subTotal" property="subTotalQty" />
        </td>
        <td>&nbsp;</td>
        <td colspan="1" width="15%" bgcolor="#FFFFFF" class="NArialL" align="right">
            <bean:write name="subTotal" property="subTotalAmt" />
        </td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</logic:iterate>
</logic:iterate>